package lanou3g.myfirstapp36kr.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;

import lanou3g.myfirstapp36kr.StringUrl;
import lanou3g.myfirstapp36kr.activity.FragmentMoneyviewpagerFragmentLvDetailsActivity;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.adapter.FragmentMoneyViewPagerFragmentListViewAdapter;
import lanou3g.myfirstapp36kr.base.BaseFragment;
import lanou3g.myfirstapp36kr.entity.FragmentMoneyViewPagerFragmentListViewEntity;
import lanou3g.myfirstapp36kr.myvolleysingleton.VolleySingleton;
import lanou3g.myfirstapp36kr.srcollView.RefreshLayout;
import lanou3g.myfirstapp36kr.srcollView.RefreshLayout.OnLoadListener;

/**
 * Created by dllo on 16/8/3.
 */
public class FragmentMoneyviewPagerFragment extends BaseFragment {
    private RefreshLayout myRefrshListView ;
    private static final String KEY_All = "all";
    private FragmentMoneyViewPagerFragmentListViewAdapter adapter;
    FragmentMoneyViewPagerFragmentListViewEntity response;
    public static FragmentMoneyviewPagerFragment newInstance(String string){
        Bundle args = new Bundle();
        args.putString(KEY_All,string);

        FragmentMoneyviewPagerFragment fragmentMoneyviewPagerFragment = new FragmentMoneyviewPagerFragment();
        fragmentMoneyviewPagerFragment.setArguments(args);
        return fragmentMoneyviewPagerFragment;
    }
    private ListView listView;
    @Override
    protected int getLayout() {
        return R.layout.fragment_money_viewpager_fragment;
    }

    @Override
    protected void initView(View view) {
        listView = (ListView) view.findViewById(R.id.fragment_money_viewpager_fragment_listview);
        myRefrshListView = (RefreshLayout) view.findViewById(R.id.swipe_layout_money);


    }

    @Override
    protected void initData() {
        adapter = new FragmentMoneyViewPagerFragmentListViewAdapter(getActivity());


        Bundle args = getArguments();
        if (args != null){
            String string = (String) args.get(KEY_All);
            VolleySingleton.addRequest(string, FragmentMoneyViewPagerFragmentListViewEntity.class,
                    new Response.Listener<FragmentMoneyViewPagerFragmentListViewEntity>() {
                        @Override
                        public void onResponse(FragmentMoneyViewPagerFragmentListViewEntity response) {
                            adapter.setBean(response);
                            FragmentMoneyviewPagerFragment.this.response = response;
                            listView.setAdapter(adapter);

                        }
                    }, new ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });



        }
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(FragmentMoneyviewPagerFragment.this.getActivity(), FragmentMoneyviewpagerFragmentLvDetailsActivity.class);
                String stringUrl = StringUrl.stringDetailsUrl + response.getData().getData().get(i).getCrowdFundingId();
                intent.putExtra("FragmentMoneyviewPagerFragmentUrl",stringUrl);
                startActivity(intent);
            }
        });
        myRefrshListView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(context, "刷新", Toast.LENGTH_SHORT).show();
                myRefrshListView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Bundle args = getArguments();
                        if (args != null){
                            String string = (String) args.get(KEY_All);
                            VolleySingleton.addRequest(string, FragmentMoneyViewPagerFragmentListViewEntity.class,
                                    new Response.Listener<FragmentMoneyViewPagerFragmentListViewEntity>() {
                                        @Override
                                        public void onResponse(FragmentMoneyViewPagerFragmentListViewEntity response) {
                                            adapter.setBean(response);
                                            FragmentMoneyviewPagerFragment.this.response = response;
                                            listView.setAdapter(adapter);

                                        }
                                    }, new ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {

                                        }
                                    });



                        }
                        myRefrshListView.setRefreshing(false);

                    }
                },1000);

            }
        });
        myRefrshListView.setmOnLoadListener(new OnLoadListener() {
            @Override
            public void onLoad() {
                Toast.makeText(context, "加载", Toast.LENGTH_SHORT).show();
                myRefrshListView.postDelayed(new Runnable() {
                    @Override
                    public void run() {


                        VolleySingleton.addRequest(StringUrl.stringMoneyMore, FragmentMoneyViewPagerFragmentListViewEntity.class,
                                new Response.Listener<FragmentMoneyViewPagerFragmentListViewEntity>() {
                                    @Override
                                    public void onResponse(FragmentMoneyViewPagerFragmentListViewEntity response) {
                                        adapter.setBean(response);
                                        FragmentMoneyviewPagerFragment.this.response = response;
                                        adapter.notifyDataSetChanged();
                                        myRefrshListView.setLoading(false);

                                    }
                                }, new ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {

                                    }
                                });

                    }
                },1500);
            }
        });




    }
}
